var config = require('config.js');

App({
  onLaunch: function () {
    let self = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        self.globalData.Location = {
          x: latitude,
          y: longitude
        }
      }
    })
  },
  request: function request({ url, method, data, success, complete, fail }) {
    if (url == '/home/order/add') {
      wx.showLoading({ title: "订单提交中..." })
    } else {
      wx.showLoading({ title: "页面加载中" })
    }
    url = config.apiUrl + url
    //console.log("请求链接：" + url)
    var method = method ? method : "GET"
    var data = data ? data : { }
    var success = success ? success : function (){ }
    var complete = complete ? complete : function (){ }
    var fail = fail ? fail : function (){ }
    //console.log(data)
    wx.request({
      url: url,
      data: data,
      method: method,
      header:{
        'content-type': 'application/json',
        'token': wx.getStorageSync("token")
      },
      success: (res) => {
        wx.hideLoading()
        if(res.code != 200){
          
        }
        success(res.data,res.header)
      },
      fail: function (res) {
        wx.hideLoading()
        wx.showToast({ title: "网络开小差了，请重新检查网络", icon: "none", duration: 3000 });
        fail(res);
      },
      complete: (res) => {
        complete(res)
      }
    })
  },
  globalData: {
    userInfo: null,
    currentCity:'北京市',
    Location:{
      x:0,
      y:0
    }
  }
})